<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <style>
        .box {
            border: 1px solid #cccccc;
        }
    </style>
</head>

<body>
    <div id="app">
        <h3>父组件</h3>
        <p>msg:{{ msg }}</p>
        <comp-a :msg=" msg "></comp-a>
        <hr>
        <comp-b @load-msg=" loadMsg "></comp-b>
    </div>
    <template id="compa">
        <div class="box">
            <h3>子组件A</h3>
            <p>msg:{{ msg }}</p>
        </div>
    </template>
    <template id="compb">
        <div class="box">
            <h3>子组件B</h3>
            <p>msg:{{ msg }}</p>
            <input type="button" value="发送msg" @click="sendMsg()">
            <input type="button" value="发送msg" @click="$emit('load-msg',msg)">

        </div>
    </template>
</body>
<script>
    Vue.component("CompA", {
        template: "#compa",
        props:{
            msg:String // 只完成类型限制其它不做定义
        }
    });
    Vue.component("CompB", {
        template: "#compb",
        data(){
            return {
                msg:"组件B的数据"
            }
        },
        methods: {
            sendMsg(){
                this.$emit("load-msg",this.msg);
            }
        },
    })
    new Vue({
        el: '#app',
        data:{
            msg:""
        },
        methods: {
            loadMsg(arg){
                this.msg = arg;
            }
        },
    });
</script>

</html>